package com.tripspins.app.fragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;

import com.tripspins.app.R;
import com.tripspins.app.adapter.TripListAdapter;
import com.tripspins.app.bo.Trip;
import com.tripspins.app.interfaces.OnFragmentInteractionListener;

import java.util.ArrayList;


public class ListTripsFragment extends Fragment {


    private OnFragmentInteractionListener mListener;
    private boolean isOnTheRoad;
    private static final String ARG_TRIPS = "arg_trips";
    private static final String ARG_ROAD = "arg_road";

    private ArrayList<Trip> listTrips;
    TripListAdapter tripListAdapter;
    public static ListTripsFragment newInstance(ArrayList<Trip> trips, boolean isOnTheRoad) {
        ListTripsFragment fragment = new ListTripsFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList(ARG_TRIPS, trips);
        args.putBoolean(ARG_ROAD, isOnTheRoad);
        fragment.setArguments(args);
        return fragment;
    }
    public ListTripsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View viewRoot =inflater.inflate(R.layout.fragment_list_trips, container, false);
        ListView listViewTrips = (ListView) viewRoot.findViewById(R.id.list_trips);

        if (getArguments() != null) {
            listTrips = getArguments().getParcelableArrayList(ARG_TRIPS);
            isOnTheRoad = getArguments().getBoolean(ARG_ROAD);
            tripListAdapter = new TripListAdapter(getActivity(), listTrips);
            try {
                listViewTrips.setAdapter(tripListAdapter);
            }catch(Exception e1){
                e1.printStackTrace();
                System.gc();
                try{
                    listViewTrips.setAdapter(tripListAdapter);
                }catch (Exception e2){e2.printStackTrace();}
            }

            listViewTrips.setEmptyView(viewRoot.findViewById(R.id.empty_list));

        }

        loadTopMenu();

        return viewRoot;
    }

    private void loadTopMenu() {
        /*if(getFragmentManager().getBackStackEntryCount() <= 1) {
            try{
                LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                if (isOnTheRoad) {
                    getActivity().getActionBar().setCustomView(inflater.inflate(R.layout.top_menu_list_trips_on, null));
                    ImageButton btnNewPin = (ImageButton) getActivity()
                            .getActionBar().getCustomView().findViewById(R.id.menu_new_pin);
                    btnNewPin.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            try {
                                view.setBackgroundColor(Color.parseColor("#6F9393"));
                            }catch(Exception e){}
                            mListener.newPin();
                        }
                    });
                    ImageButton btnFinish = (ImageButton) getActivity().findViewById(R.id.menu_finish);
                    btnFinish.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mListener.finishTrip();
                        }
                    });
                } else {
                    getActivity().getActionBar().setCustomView(inflater.inflate(R.layout.top_menu_list_trips_off, null));
                    ImageButton btnNewTrip = (ImageButton) getActivity()
                            .getActionBar().getCustomView().findViewById(R.id.menu_new_trip);
                    btnNewTrip.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            try {
                                view.setBackgroundColor(Color.parseColor("#6F9393"));
                            }catch (Exception e){}
                            mListener.newTrip();
                        }
                    });
                }
            }catch (Exception e){}
        }*/
    }

    public void reloadTrips(ArrayList<Trip> trips)
    {
        listTrips.clear();
        listTrips.addAll(trips);

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putParcelableArrayList(ARG_TRIPS, listTrips);
        outState.putBoolean(ARG_ROAD, isOnTheRoad);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
        super.onCreateOptionsMenu(menu,menuInflater);
        menu.clear();
        if(isOnTheRoad)
            menuInflater.inflate(R.menu.menu_on_the_road, menu);
        else
            menuInflater.inflate(R.menu.menu_off_the_road, menu);

    }




}
